var site_url=localStorage.getItem("site_url");
function getParam(){
	//当前页数
	var pageNo=$('#pageNo').val();
	//总页数
	var total_page=$('#total_page').val();
	//总条数
	var total_rows=$('#total_rows').val();
	var keyword=$('#keyword').val();
	
	var deptId=$("#search_department").val(); 
	var start=$('#search_date_start').val();
	
	var end=$('#search_date_end').val();
	
	var keyword=$('#search_keyword').val();
	var userDepartId=localStorage.getItem("departId"); 
	var param = {
		current:pageNo,
	 	deptId:deptId==undefined?userDepartId:deptId,
	 	start:start,
	 	end:end,
	 	keyword:keyword
	};
	return param;    
}
function pageStart(type){
	// getData();
	//location.href="";
	getProject();

	//当前页数
	var pageNo=$('#pageNo').val();
	//总页数
	var total_page=$('#total_page').val();
	var keyword=$('#keyword').val();
	
    
    var projectId=$("#search_project option:selected").val(); 
    
    var deptId=$('#search_department').val();
	
    var start=$('#search_date_start').val();
    
    var end=$('#search_date_end').val();
    
    var keyword=$('#search_keyword').val();
 	var param = {
    	current:pageNo,
     	deptId:deptId==undefined?"":deptId,
     	start:start,
     	end:end,
     	keyword:keyword
 	};
    
    switch(type){
    	//重要通知
    	case 2:
			var userInfo = getUserInfo();
			var dept = getDeptById(userInfo.deptId);
			
			for(var i = 0;i<dept.length;i++){
				var gaoji,querynames = "";
				if (dept[i].type == 2) {
					gaoji = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
				}
				
				if (dept[i].type == 3){
					if(userInfo.deptId==dept[i].id){
						querynames = $('<option value="'+dept[i].id+'" selected="selected">'+dept[i].name+'</option>');
					}else{
						querynames = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
					}
				}
				
				$("#search_project").append(gaoji);
				$("#search_department").append(querynames);
			}
    		getDataNotice(param);
    		break;	
    	//问题反馈(反馈列表)
    	case 61:
			var userInfo = getUserInfo();
			var dept = getDeptById(userInfo.deptId);
			
			for(var i = 0;i<dept.length;i++){
				var gaoji,querynames = "";
				if (dept[i].type == 2) {
					gaoji = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
				}
				
				if (dept[i].type == 3){
					if(userInfo.deptId==dept[i].id){
						querynames = $('<option value="'+dept[i].id+'" selected="selected">'+dept[i].name+'</option>');
					}else{
						querynames = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
					}
				}
				
				$("#search_project").append(gaoji);
				$("#search_department").append(querynames);
			}
    		getDataFeedback(param);
    		break;	
    	//问题反馈(已配任务)
    	case 62:
			var userInfo = getUserInfo();
			var dept = getDeptById(userInfo.deptId);
			
			for(var i = 0;i<dept.length;i++){
				var gaoji,querynames = "";
				if (dept[i].type == 2) {
					gaoji = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
				}
				
				if (dept[i].type == 3){
					if(userInfo.deptId==dept[i].id){
						querynames = $('<option value="'+dept[i].id+'" selected="selected">'+dept[i].name+'</option>');
					}else{
						querynames = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
					}
				}
				
				$("#search_project").append(gaoji);
				$("#search_department").append(querynames);
			}
    		getDataTask(param);
    		break;	
    	//招聘计划
    	case 8:
			var userInfo = getUserInfo();
			var dept = getDeptById(userInfo.deptId);
			
			for(var i = 0;i<dept.length;i++){
				var gaoji,querynames = "";
				if (dept[i].type == 2) {
					gaoji = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
				}
				
				if (dept[i].type == 3){
					if(userInfo.deptId==dept[i].id){
						querynames = $('<option value="'+dept[i].id+'" selected="selected">'+dept[i].name+'</option>');
					}else{
						querynames = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
					}
				}
				
				$("#search_project").append(gaoji);
				$("#search_department").append(querynames);
			}
    		getDataRecruit(param);
    		break;	
    	//政策文件
    	case 9:
    		getDataPolicy(param);
    		break;
		//设备申请
		case 10:
			var userInfo = getUserInfo();
			var dept = getDeptById(userInfo.deptId);
			
			for(var i = 0;i<dept.length;i++){
				var gaoji,querynames = "";
				if (dept[i].type == 2) {
					gaoji = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
				}
				
				if (dept[i].type == 3){
					if(userInfo.deptId==dept[i].id){
						querynames = $('<option value="'+dept[i].id+'" selected="selected">'+dept[i].name+'</option>');
					}else{
						querynames = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
					}
				}
				
				$("#search_project").append(gaoji);
				$("#search_department").append(querynames);
			}
			getDataEquipment(param);
			break;
		case 11:
			getDataTrain(param);
			break;
    }	
/* 	  //分页
	$("#page").paging({
	    pageNo: parseInt(pageNo),
	    totalPage: parseInt(total_page),
	    // totalSize: total_rows,
	    callback: function(num) {
	
	    }
	}) */
}
/* 项目部赋值 */
function getProject(){
	var URL = site_url+"/depts/all";
	var userToken=localStorage.getItem("userToken");
	var userInfo=getUserInfo();
	var userDepartId=userInfo.deptId;
	$.ajax({
		type:"get",
		url:URL,
		async:false,
		headers:{'token':userToken},
		success:function(data){
			
			var allSave = "";
			var allEdit = "";
			var feedback= "";
			for(var i = 0;i<data.length;i++){
				
				allSave = $('<option value="'+data[i].id+'">'+data[i].name+'</option>');
				allEdit = $('<option value="'+data[i].id+'">'+data[i].name+'</option>');
				feedback = $('<option value="'+data[i].id+'">'+data[i].name+'</option>');
				$("#department-edit-recutit").append(allEdit);
				$("#departmentEdit").append(feedback);
				$("#department-save-recutit").append(allSave);
				$("#departmentSave").append(allSave)
			}
		}
	});
}

/*根据用户所在部门查询所有部门*/
function getDeptById(id){
	$("#search_project").val();
	$("#search_department").val();
	var result=[];
	var userToken = localStorage.getItem('userToken');
	$.ajax({
		url:site_url+"/depts/children?id="+id,
		dataType:'json',
		type:'GET',
		async:false,
		headers:{'token':userToken},
		success:function(data){
			result = data.data
		}
	})
	return result;
}

//给重要通知页面赋值
function getDataNotice(param){
	
	var URL=site_url+"/notices/page?size=10&current="+param.current+"&deptId="+param.deptId+"&start="+param.start+"&end="+param.end+"&theme="+param.keyword;
	var userToken = localStorage.getItem('userToken');

	$.ajax({
	    type: 'GET',
	    url: URL,
	    dataType:"json",
		headers:{'token':userToken},
	    success: function (json) {
			var data=json.data.records;
			$('#data').html("");
			var html="";
			html+='<table>';
			html+='<tr class="table-top">';
			html+='<td width="30%">标题</td><td width="30%">时间</td><td width="15%">权重</td><td width="25%">操作</td></tr>';
	        for (var i=0;i<data.length;i++) {
	        	if(i%2==0){
	        		html+='<tr class="item-white" onclick="dateEdit(7,'+data[i].id+')">';
	        	}else{
	        		html+='<tr class="item-blue" onclick="dateEdit(7,'+data[i].id+')">';
	        	}
	        	html+='<td>'+data[i].theme+'</td>';
	        	html+='<td>'+data[i].createTime+'</td>';
	        	html+='<td>'+data[i].weight+'</td>';
	        	html+='<td>';
	        	html+='<img id="edit" onclic src="img/edit_icon.png" onclick="dateEdit(1,'+data[i].id+')"/>';
	        	html+='<img id="delete" style="margin-left: 10px;" src="img/delete_icon.png" onclick="dateDelete(1,'+data[i].id+')"/>';
	        	html+='</td>';
	        	html+='</tr>';
	        }
			if(data.length<=0){
				html+='<tr class="item-white">';
				html+='<td colspan="4"><div>暂无数据</div></td>';
				html+='</tr>';
			}else{
				var pageNo=json.data.current;
				var total_page=json.data.pages;
				localStorage.setItem("total_page",total_page);
				//分页
				$("#page").paging({
				    pageNo: parseInt(pageNo),
				    totalPage: parseInt(total_page),
				    callback: function(num) {
						$('#pageNo').val(num);
						var total_page=localStorage.getItem("total_page");
						$("#page").paging({
						    pageNo: parseInt(num),
						    totalPage: parseInt(total_page)
						})
						var paramData=getParam();
						getDataNotice(paramData);
						$("#page").stopPagination({});
				    }
				})
			}
			html+='</table>';
			$('#data').html(html);
			
			
	    }
	});  	
}
//给反馈列表页面赋值
function getDataFeedback(param){
	var userToken=localStorage.getItem("userToken");
	var URL = site_url+"/handclaps/page?size=10&current="+param.current+"&deptId="+param.deptId+"&start="+param.start+"&end="+param.end+"&keyword="+param.keyword;
	$.ajax({
		type:"get",
		url:URL,
		headers:{'token':userToken},
		success:function(data){
			var result = data.data.records;
			
			var html="";
			html+='<table>';
			html+='<tr class="table-top">';
			html+='<td width="10%">序号</td><td width="20%">问题</td><td width="15%">发起人</td><td width="15%">地点</td><td width="20%">时间</td><td width="20%">操作</td></tr>';
		
			for (var i=0;i<result.length;i++) {
				if(i%2==0){
					html+='<tr class="item-white" onclick="dateEdit(2,\''+result[i].id+'\')">';
				}else{
					html+='<tr class="item-blue" onclick="dateEdit(2,\''+result[i].id+'\')">';
				}
				html+='<td>'+(i+1)+'</td>';
				html+='<td>'+result[i].problem+'</td>';
				html+='<td>'+result[i].userName+'</td>';
				html+='<td>'+result[i].position+'</td>';
				html+='<td>'+result[i].createTime+'</td>';
				html+='<td>';
				html+='<img id="edit" onclic src="img/handle_icon.png" onclick="dateEdit(2,\''+result[i].id+'\')"/>';
				html+='</td>';
				html+='</tr>';
						
			}
			if(result.length<=0){
				html+='<tr class="item-white">';
				html+='<td colspan="6"><div>暂无数据</div></td>';
				html+='</tr>';
			}else{
				var pageNo=data.data.current;
				var total_page=data.data.pages;
				localStorage.setItem("total_page",total_page);
				//分页
				$("#page").paging({
				    pageNo: parseInt(pageNo),
				    totalPage: parseInt(total_page),
				    callback: function(num) {
						$('#pageNo').val(num);
						var total_page=localStorage.getItem("total_page");
						$("#page").paging({
						    pageNo: parseInt(num),
						    totalPage: parseInt(total_page)
						})
						var paramData=getParam();
						getDataFeedback(paramData);
						$("#page").stopPagination({});
				    }
				})
			}
			html+='</table>';
			$('#data').html(html);
			
		}
	});
	
}
//给已配任务页面赋值
function getDataTask(param){
	var userToken=localStorage.getItem("userToken");
	var URL = site_url+"/handclaps/page/distributed?size=10&current="+param.current+"&deptId="+param.deptId+"&start="+param.start+"&end="+param.end+"&keyword="+param.keyword+"&result=0"
	$.ajax({
		type:"get",
		url:URL,
		headers:{'token':userToken},
		success:function(data){
			var result = data.data.records;
			$('#data').html("");
			var html="";
			html+='<table>';
			html+='<tr class="table-top">';
			html+='<td width="10%">序号</td><td width="15%">问题</td><td width="10%">发起人</td><td width="10%">地点</td><td width="10%">分配人</td>';
			html+='<td width="10%">完成人</td><td width="10%">完成时间</td><td width="10%">状态</td><td width="15%">操作</td></tr>';
		
			for (var i=0;i<result.length;i++) {
				if(i%2==0){
					html+='<tr class="item-white" onclick="dateEdit(3,\''+result[i].id+'\')">';
				}else{
					html+='<tr class="item-blue" onclick="dateEdit(3,\''+result[i].id+'\')">';
				}
				var start =  result[i].finish?"完成":"未完成";
				html+='<td>'+(i+1)+'</td>';
				html+='<td>'+(result[i].problem==null?"":result[i].problem)+'</td>';
				html+='<td>'+(result[i].userName==null?"":result[i].userName)+'</td>';
				html+='<td>'+(result[i].position==null?"":result[i].position)+'</td>';
				html+='<td>'+(result[i].distributor==null?"":result[i].distributor)+'</td>';
				html+='<td>'+(result[i].finishName==null?"":result[i].finishName)+'</td>';
				html+='<td>'+(result[i].finishTime==null?"":result[i].finishTime)+'</td>';
				html+='<td>'+start+'</td>';
				html+='<td>';
				html+='<img id="check" onclic src="img/check_icon.png" onclick="dateEdit(3,\''+result[i].id+'\')"/>';
				html+='</td>';
				html+='</tr>';
						
			}
			if(result.length<=0){
				html+='<tr class="item-white">';
				html+='<td colspan="8"><div>暂无数据</div></td>';
				html+='</tr>';
			}else{
			
				var pageNo=data.data.current;
				var total_page=data.data.pages;
				localStorage.setItem("total_page",total_page);
				//分页
				$("#page").paging({
				    pageNo: parseInt(pageNo),
				    totalPage: parseInt(total_page),
				    callback: function(num) {
						$('#pageNo').val(num);
						var total_page=localStorage.getItem("total_page");
						$("#page").paging({
						    pageNo: parseInt(num),
						    totalPage: parseInt(total_page)
						})
						var paramData=getParam();
						getDataTask(paramData);
						$("#page").stopPagination({});
				    }
				})
			}
			html+='</table>';
			$('#data').html(html);
		}
	})
}

/* 添加页面 */
function add(){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
	    
		$("#addDiv").show();
	});
}
/* 编辑页面 */
function dateEdit(type,id){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
	    var userToken = localStorage.getItem("userToken");
			switch(type){
				//重要通知页面
				case 1:
					var URL=site_url+"/notices";
					// alert(URL);
					var data={
						id:id
					};
					$.ajax({
					    type: 'GET',
					    url: URL,
					    dataType:"json",
					    data: data,
					    headers:{'token':userToken},
					    success: function (json) {
							var data=json.data;
							// alert(data.content);
							$("#noticeId").val(data.id); 
							$("#title_edit").val(data.theme);     
							$("#weight_edit").val(data.weight); 
							// document.getElementById("content_edit").value =data.content;
							editorEdit.html(data.content);
							$("#content_edit").html(data.content); 
					        //保存成功
					    }
					}); 
					
					$("#editDiv").show();
					// layer.alert("重要通知编辑！"+id, {icon: 6});
					break;	
				//问题反馈编辑页面
				case 2:
				
					var URL=site_url+"/handclaps";
					// alert(URL);
					var data={
						id:id
					};
					$.ajax({
					    type: 'GET',
					    url: URL,
					    dataType:"json",
					    data: data,
					    headers:{'token':userToken},
					    success: function (json) {
							var data=json.data;
							$("#desc_edit").text(data.problem);     
							$("#feedback_img").html("");
							var querynames = "";
							for(var i=0;i<data.pictures.length;i++){
								querynames = $('<img style="width:80px; margin-left: 20px;" src="'+data.pictures[i]+'"/>');
								$("#feedback_img").append(querynames);
							}
							
							/* var img = data.picture.split(",");
							var img = [];
							if (data.pictures.indexOf(",") != -1) {
								img = data.pictures.split(",");
								for(var i=0;i<img.length;i++){
									var querynames = $('<img style="width:80px; margin-left: 20px;" src="'+img[i]+'"/>');
								}
								$("#feedback_img").append(querynames);
							}else{
								$("#feedback_img").append($('<img style="width:80px; margin-left: 20px;" src="'+img+'"/>'));
							} */
							
							
							$("#feedbackId").val(id);
					    }
					}); 
					$("#editDiv").show();
					break;
				//已配任务编辑页面
				case 3:
				
				
				var URL=site_url+"/handclaps";
				// alert(URL);
				var data={
					id:id
				};
				$.ajax({
				    type: 'GET',
				    url: URL,
				    dataType:"json",
				    data: data,
				    headers:{'token':userToken},
				    success: function (json) {
						var resp=json.data;
						$("#name_edit").text(resp.distributor);  
						$("#complete_date_edit").text(resp.finishTime==null?'':resp.finishTime);  
						$("#desc_edit").text(resp.problem);    
						 $("#questionImg").html("");
						 var querynames = "";
						 if (resp.pictures!=null && resp.pictures!="") {
							for(var i=0;i<resp.pictures.length;i++){
							 	if(resp.pictures[i]!=""){
							 		querynames = $('<img id="feedback_img1" class="popup-item-img" style="margin-left: 40px;" src="'+resp.pictures[i]+'"/>');
							 		$("#questionImg").append(querynames);
							 	}
							 	
							}
						 }
						 
						/* if (resp.picture!=null && resp.picture!="") {
							var img = resp.picture.split(",");
							if(img.length>0){
								$("#questionImg").html("");
								var html="";
								for(var i=0;i<img.length;i++){
									if(img[i]!=""){
										html='<img id="feedback_img1" class="popup-item-img" style="margin-left: 40px;" src="'+img[i]+'"/>';
									}
								}
								$("#questionImg").append(html);
							}
						} */
						if (resp.finishPic!=null && resp.finishPic!="") {
							$("#taskImg").html("");
							for(var i=0;i<resp.finishPic.length;i++){
								if(resp.finishPic[i]!=""){
									querynames = $('<img id="task_img1" class="popup-item-img" style="margin-left: 40px;" src="'+resp.finishPic[i]+'"/>');
									$("#taskImg").append(querynames);
								}
							}
						}
						
					/* 	if (resp.finishPic!=null && resp.finishPic!="") {
							var finishImg = resp.finishPic.split(",");
							if(finishImg.length>0){
								$("#taskImg").html("");
								var html="";
								for(var i=0;i<finishImg.length;i++){
									if(finishImg[i]!=""){
										html='<img id="feedback_img1" class="popup-item-img" style="margin-left: 40px;" src="'+finishImg[i]+'"/>';
									}
								}
								$("#taskImg").append(html);
							}
						} */
						
						$("#editDiv").show();
				    }
				}); 
					
					// layer.alert("重要通知编辑！"+id, {icon: 6});
					break;
				//招聘计划编辑页面
				case 4:
					
					getRecruitById(id);
					
					// $("#departmentEdit").val("客服部");       //所属部门
					// $("#positionEdit").val("客服专员");       //职位
					// $("#educationEdit").val("专科");          //内容
					// $(':radio[name="fullTimeEdit"]:checked').val();//是否全日制
					// $("#salaryStartEdit").val("4");     //月薪开始值
					// $("#salaryEndEdit").val("5");       //月薪结束值
					// $("#recruitDateEdit").val("2019-9-10");   //招聘时间
					// $("#recruitNumEdit").val("10");           //招聘人数
					// $("#descEdit").val("职位描述！");          //职位描述
					
					$("#editDiv").show();
					// layer.alert("重要通知编辑！"+id, {icon: 6});
					break;
				//政策文件编辑页面
				case 5:
					$.ajax({
					    type: 'GET',
					    url: site_url+"/policys/"+id,
					    dataType:"json",
					    data: data,
					    headers:{'token':userToken},
					    success: function (json) {
							var data=json.data;
							$('#themeEdit').val(data.title);   //标题
							$('#uploadEdit').val(data.userName);            //上传者
							$('#weightEdit').val(data.weight);               //权重
							// $('#fileSrc').attr("href",data.file);
							$('#fileSrcSave').val(data.file);
							$("#policyIdEdit").val(data.id);
							$("#editDiv").show();
					    }
					});
					
					break;
				//申请设备编辑页面
				case 6:
					$("#departmentEdit").html("");
					var userInfo = getUserInfo();
					
					var dept = getDeptById(userInfo.deptId);
					// alert(dept.length);
					for(var i = 0;i<dept.length;i++){
						if (dept[i].type !=1) {
							$("#departmentEdit").append($('<option value="'+dept[i].id+'">'+dept[i].name+'</option>'))
						}
					}
					
					
					$.ajax({
						type:"get",
						url:site_url+"/deviceapplys?id="+id,
						headers:{'token':userToken},
						success:function(data){
							$("#deviceapplys_id").val(id);
							$('#departmentEdit').val(data.data.deptId);           //部门
							$('#usernameEdit').val(data.data.userName);               //姓名
							$('#phoneEdit').val(data.data.phone);           //联系电话
							$('#borrowEdit').val(data.data.loanedGoods);                //借出物品
							$('#borrowTimeEdit').val(data.data.loanedTime);  //借出时间
							$('#returnTimeEdit').val(data.data.backTime);  //归还时间
							$('#statusEdit').val(data.data.state);                  //状态
							$('#reasonEdit').val(data.data.reason);              //原因
							getUserByDept();
							$("#editDiv").show();
						}
					});
					
					break;
					//重要通知详情页面
					case 7:
						var URL=site_url+"/notices";
						// alert(URL);
						var data={
							id:id
						};
						$.ajax({
						    type: 'GET',
						    url: URL,
						    dataType:"json",
						    data: data,
						    headers:{'token':userToken},
						    success: function (json) {
								var data=json.data;
								// alert(data.content);
								$("#theme_info").val(data.theme);     
								$("#weight_info").val(data.weight); 
								// document.getElementById("content_edit").value =data.content;
								editorInfo.html(data.content);
								$("#content_info").html(data.content); 
						        //保存成功
						    }
						}); 
						
						$("#infoDiv").show();
						// layer.alert("重要通知编辑！"+id, {icon: 6});
						break;
				default:
					break;
			}	
	    
	});
}
/* 删除 */
function dateDelete(type,id){
	event.stopPropagation();
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
	    
	    var userToken=localStorage.getItem("userToken");
	    var data={
			id:id
		};
		switch(type){
			//重要通知页面
			case 1:
				var URL=site_url+"/notices?id="+id;
				// alert(id);
				layer.confirm('确定删除该数据吗？', { icon: 3, title: '提示',shade:0 }, function (index) {
					$.ajax({
						type: 'DELETE',
						url: URL,
						dataType:"json",
						headers:{'token':userToken},
						success: function (json) {
							var res=json.code;
							// alert(json);
							if(res==0){
								layer.alert("删除成功！", {icon: 6,shade:0});
								location.reload();
							}else if(res==1){
								layer.alert("删除失败！", {icon: 5,shade:0});
							}
						}
					});
					layer.close(index);
				});
		
				break;	
			//招聘计划删除
			case 2:
				var URL=site_url+"/recruitmentplans?id="+id;
				layer.confirm('确定删除该数据吗？', { icon: 3, title: '提示',shade:0 }, function (index) {
					$.ajax({
						type: 'DELETE',
						url: URL,
						dataType:"json",
						headers:{'token':userToken},
						success: function (json) {
							var res=json.code;
							// alert(json);
							if(res==0){
								layer.alert("删除成功！", {icon: 6,shade:0});
								location.reload();
							}else if(res==1){
								layer.alert("删除失败！", {icon: 5,shade:0});
							}
						}
					});
					layer.close(index);
				});
					
				break;	
			/* 培训记录删除 */
			case 3:
				var URL=site_url+"/trains?id="+id;
				layer.confirm('确定删除该数据吗？', { icon: 3, title: '提示',shade:0 }, function (index) {
					$.ajax({
						type: 'DELETE',
						url: URL,
						dataType:"json",
						headers:{'token':userToken},
						success: function (json) {
							var res=json.code;
							// alert(json);
							if(res==0){
								layer.alert("删除成功！", {icon: 6,shade:0});
								location.reload();
							}else if(res==1){
								layer.alert("删除失败！", {icon: 5,shade:0});
							}
						}
					});
					layer.close(index);
				});
				break;
			/* 培训记录的图片删除 */
			case 4:
				layer.confirm('确定删除该图片吗？', { icon: 3, title: '提示',shade:0 }, function (index) {
					layer.alert("删除成功！", {icon: 6,shade:0});
					// num=num-1;
					trainImg.splice(id,1);
					// trainImg[id]="";
					getImgData();
					layer.close(index);
				});
				break;
			case 8:
				var URL=site_url+"/policys?id="+id;
				layer.confirm('确定删除该数据吗？', { icon: 3, title: '提示',shade:0 }, function (index) {
					$.ajax({
						type: 'DELETE',
						url: URL,
						dataType:"json",
						headers:{'token':userToken},
						success: function (json) {
							var res=json.code;
							// alert(json);
							if(res==0){
								layer.alert("删除成功！", {icon: 6,shade:0});
								location.reload();
							}else if(res==1){
								layer.alert("删除失败！", {icon: 5,shade:0});
							}
						}
					});
					layer.close(index);
				});
				break;
			default:
				break;
		}	
	});
}
/* 重要通知添加功能的保存按钮 */
function noticeAddSave(){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
		
		var theme=$("#theme").val();       //主题
		var weight=$("#weight").val();     //权重
		var content=$("#content").val();   //内容
		
		if(theme==""){
			layer.alert("主题不能为空！", {icon: 5});
			return;
		}
		if(weight==""){
			layer.alert("权重不能为空！", {icon: 5});
			return;
		}

		if(content==""){
			layer.alert("内容不能为空！", {icon: 5});
			return;
		}
        var URL=site_url+"/notices";
	 	// alert(content);
        var userInfo = getUserInfo();
        // alert(URL);
        var data={
        	userId:userInfo.id,
        	deptId:userInfo.deptId,
            theme:theme,
			weight:weight,
			content:content
        };
        
        var userToken = localStorage.getItem("userToken");
        
        $.ajax({
            type: 'POST',
            url: URL,
            dataType:"json",
			contentType:"application/json",
            data: JSON.stringify(data),
            headers:{'token':userToken},
            success: function (json) {
				var res=json.code;
				// alert(json);
                //保存成功
                if(res==0){
                    layer.alert("添加成功！", {icon: 6,shade:0});
					$("#theme").val(""); 
					$("#weight").val(""); 
					$("#content").val(""); 
					$("#addDiv").hide();
					location.reload();
                }else{
                    layer.alert(json.msg, {icon: 5,shade:0});
                }
            }
        }); 
	});
}
/* 重要通知编辑功能的保存按钮 */
function noticeEditSave(){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
		var id=$("#noticeId").val();   
		var theme=$("#title_edit").val();       //主题
		var weight=$("#weight_edit").val();     //权重
		var content=$("#content_edit").val();   //内容
		
		if(theme==""){
			layer.alert("主题不能为空！", {icon: 5});
			return;
		}
		if(weight==""){
			layer.alert("权重不能为空！", {icon: 5});
			return;
		}

		if(content==""){
			layer.alert("内容不能为空！", {icon: 5});
			return;
		}
		var URL=site_url+"/notices";
		
		var user = getUserInfo();
		
		var data={
			id:id,
			userId:user.id,
			deptId:user.deptId,
		    theme:theme,
			weight:weight,
			content:content
		};
		var userToken = localStorage.getItem("userToken");
		
		$.ajax({
		    type: 'PUT',
		    url: URL,
		    dataType:"json",
			contentType:"application/json",
		    data: JSON.stringify(data),
		    headers:{'token':userToken},
		    success: function (json) {
				var res=json.code;
				// alert(json);
		        //保存成功
		        if(res==0){
		            layer.alert("编辑成功！", {icon: 6,shade:0});
					location.reload();
		        }else if(res==1){
		            layer.alert("编辑失败！", {icon: 5,shade:0});
		        }
		    }
		}); 
		
	});
}

/* 获取用户信息 */
function getUserInfo(){

		var userToken=localStorage.getItem("userToken");
		var URL=site_url+"/login/user";
		var user="";
		$.ajax({
		    type: 'GET',
		    url: URL,
		    dataType:"json",
			headers:{'token':userToken},
			async:false,
		    success: function (json) {
				user=json.data;
		    }
		});
		return user;
}
//给招聘计划页面赋值
function getDataRecruit(param){
	
	var userToken=localStorage.getItem("userToken");
	var URL = site_url+"/recruitmentplans/page?size=10&current="+param.current+"&deptId="+param.deptId+"&start="+param.start+"&end="+param.end+"&keyword="+param.keyword;
	
	$.ajax({
		type:"get",
		url:URL,
		headers:{'token':userToken},
		success:function(data){
			var result = data.data.records;
			$('#data').html("");
			var html="";
			html+='<table>';
			html+='<tr class="table-top">';
			html+='<td width="10%">部门</td><td width="10%">职位</td><td width="10%">学历</td><td width="10%">工资</td><td width="10%">工作年限</td>';
			html+='<td width="20%">招聘时间</td><td width="10%">招聘人数</td><td width="20%">操作</td></tr>';
		
			for (var i=0;i<result.length;i++) {
				if(i%2==0){
					html+='<tr class="item-white" >';
				}else{
					html+='<tr class="item-blue">';
				}
				html+='<td>'+result[i].deptName+'</td>';
				html+='<td>'+result[i].job+'</td>';
				html+='<td>'+result[i].education+'</td>';
				html+='<td>'+result[i].salary+'</td>';
				html+='<td>'+result[i].workYears+'</td>';
				html+='<td>'+result[i].recruitmentTime+'</td>';
				html+='<td>'+result[i].peoples+'</td>';
				html+='<td>';
				html+='<img src="img/edit_icon.png" onclick="dateEdit(4,\''+result[i].id+'\')"/>';
				html+='<img style="margin-left: 10px;" src="img/delete_icon.png"  onclick="dateDelete(2,\''+result[i].id+'\')"/>';
				html+='</td>';
				html+='</tr>';
			}
			if(result.length<=0){
				html+='<tr class="item-white">';
				html+='<td colspan="8"><div>暂无数据</div></td>';
				html+='</tr>';
			}else{
				var pageNo=data.data.current;
				var total_page=data.data.pages;
				localStorage.setItem("total_page",total_page);
				//分页
				$("#page").paging({
				    pageNo: parseInt(pageNo),
				    totalPage: parseInt(total_page),
				    callback: function(num) {
						$('#pageNo').val(num);
						var total_page=localStorage.getItem("total_page");
						$("#page").paging({
						    pageNo: parseInt(num),
						    totalPage: parseInt(total_page)
						})
						var paramData=getParam();
						getDataRecruit(paramData);
						$("#page").stopPagination({});
				    }
				})
			}
			html+='</table>';
			$('#data').html(html);
			
		}
	})
	
}
/* 招聘计划添加功能的保存按钮 */
function recruitAddSave(){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
		
		var department=$("#department-save-recutit").val();       //所属部门
		var job=$("#position").val();     //职位
		var education=$("#education").val();  
		var fullTime=$(':radio[name="fullTime"]:checked').val();//是否全日制
		var salaryStart=$("#salaryStart").val();   //月薪开始值
		var salaryEnd=$("#salaryEnd").val();       //月薪结束值
		var recruitDate=$("#recruitDate").val();   //招聘时间
		var recruitNum=$("#recruitNum").val();     //招聘人数
		var desc=$("#desc").val(); //职位描述
		
		var workYear = $("#workYear").val();
		var unlimited = $("#unlimited").val();
		var negotiation = $("#negotiation").val();
		
		if(department==""){
			layer.alert("所属部门不能为空！", {icon: 5});
			return;
		}
		if(position==""){
			layer.alert("职位名称不能为空！", {icon: 5});
			return;
		}
		if(education==""){
			layer.alert("学历不能为空！", {icon: 5});
			return;
		}
		if(salaryStart==""){
			layer.alert("月薪开始值不能为空！", {icon: 5});
			return;
		}
		if(salaryEnd==""){
			layer.alert("月薪结束值不能为空！", {icon: 5});
			return;
		}
		if(recruitDate==""){
			layer.alert("招聘时间不能为空！", {icon: 5});
			return;
		}
		if(recruitNum==""){
			layer.alert("招聘人数不能为空！", {icon: 5});
			return;
		}
		if(desc==""){
			layer.alert("职位描述不能为空！", {icon: 5});
			return;
		}
		
        var URL=site_url+"/recruitmentplans";
        
        var data={
        	deptId:department,
            job:job,
			education:education,
			salary:salaryStart+"-"+salaryEnd,
			workYears:workYear,
			peoples:recruitNum,
			recruitmentTime:recruitDate,
			content:desc
        };
    	var userToken=localStorage.getItem("userToken");
        $.ajax({
            type: 'POST',
            url: URL,
            dataType:"json",
			contentType:"application/json",
			headers:{'token':userToken},
            data: JSON.stringify(data),
            success: function (json) {
				var res=json.code;
				// alert(json);
                //保存成功
                if(res==0){
                    layer.alert("添加成功！", {icon: 6,shade:0});
					location.reload();
                }else if(res==1){
                    layer.alert("添加失败！", {icon: 5,shade:0});
                }
            }
        }); 
	});
}
/* 招聘计划编辑功能的保存按钮 */
function recruitEditSave(){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
		
		var id = $("#recruitIdEdit").val();
		var department=$("#department-edit-recutit").val();       //所属部门
		var position=$("#positionEdit").val();     //职位
		var education=$("#educationEdit").val();   //内容
		var fullTime=$(':radio[name="fullTimeEdit"]:checked').val();//是否全日制
		var salaryStart=$("#salaryStartEdit").val();   //月薪开始值
		var salaryEnd=$("#salaryEndEdit").val();       //月薪结束值
		var recruitDate=$("#recruitDateEdit").val();   //招聘时间
		var recruitNum=$("#recruitNumEdit").val();     //招聘人数
		var desc=$("#descEdit").val();                 //职位描述
		var workYear = $("#workYearEdit").val();
		
		if(department==""){
			layer.alert("所属部门不能为空！", {icon: 5});
			return;
		}
		if(position==""){
			layer.alert("职位名称不能为空！", {icon: 5});
			return;
		}
		if(education==""){
			layer.alert("学历不能为空！", {icon: 5});
			return;
		}
		if(salaryStart==""){
			layer.alert("月薪开始值不能为空！", {icon: 5});
			return;
		}
		if(salaryEnd==""){
			layer.alert("月薪结束值不能为空！", {icon: 5});
			return;
		}
		if(recruitDate==""){
			layer.alert("招聘时间不能为空！", {icon: 5});
			return;
		}
		if(recruitNum==""){
			layer.alert("招聘人数不能为空！", {icon: 5});
			return;
		}
		if(desc==""){
			layer.alert("职位描述不能为空！", {icon: 5});
			return;
		}
		
        var URL=site_url+"/recruitmentplans";
        
       var data={
       		id:id,
        	deptId:department,
            job:position,
			education:education,
			salary:salaryStart+"-"+salaryEnd,
			workYears:workYear,
			peoples:recruitNum,
			recruitmentTime:recruitDate,
			content:desc
        };
        var userToken=localStorage.getItem("userToken");
        
        $.ajax({
            type: 'PUT',
            url: URL,
            dataType:"json",
			contentType: 'application/json; charset=utf-8',
			headers:{'token':userToken},
            data: JSON.stringify(data),
            success: function (json) {
				var res=json.code;
				// alert(json);
                //保存成功
                if(res==0){
                    layer.alert("修改成功！", {icon: 6,shade:0});
					location.reload();
                }else if(res==1){
                    layer.alert("修改失败！", {icon: 5,shade:0});
                }
            }
        }); 
	});
}
//给政策文件页面赋值
function getDataPolicy(param){
	$('#data').html("");
	
	var userToken = localStorage.getItem("userToken");
	
	$.ajax({
		type:"get",
		url:site_url+"/policys?size=10&current="+param.current+"&start="+param.start+"&end="+param.end+"&keyword="+param.keyword,
		headers:{'token':userToken},
		success:function(data){
			var result = data.data.records;
	
			$('#pageNo').val(data.data.current);
			var total_page=data.data.total/9;
			$('#data_overtime').html("");
			var html="";
			html+='<table>';
			html+='<tr class="table-top">';
			html+='<td width="25%">标题</td><td width="15%">时间</td><td width="15%">上传者</td><td width="15%">权重</td><td width="15%">下载正文</td><td width="15%">操作</td></tr>';
			
			for (var i=0;i<result.length;i++) {
				if(i%2==0){
					html+='<tr class="item-white" >';
				}else{
					html+='<tr class="item-blue">';
				}
				html+='<td>'+result[i].title+'</td>';
				html+='<td>'+result[i].createTime+'</td>';
				html+='<td>'+result[i].userName+'</td>';
				html+='<td>'+result[i].weight+'</td>';
				html+='<td><div id="download"><img src="img/download_icon.png" onclick="downloadPolicy(\''+result[i].file+'\');"/></div></td>';
				html+='<td>';
				html+='<img src="img/edit_icon.png" onclick="dateEdit(5,\''+result[i].id+'\')"/>';
				html+='<img style="margin-left: 10px;" src="img/delete_icon.png"  onclick="dateDelete(8,\''+result[i].id+'\')"/>';
				html+='</td>';
				html+='</tr>';
			}
			if(result.length<=0){
				html+='<tr class="item-white">';
				html+='<td colspan="6"><div>暂无数据</div></td>';
				html+='</tr>';
			}else{
				var pageNo=data.data.current;
				var total_page=data.data.pages;
				localStorage.setItem("total_page",total_page);
				//分页
				$("#page").paging({
				    pageNo: parseInt(pageNo),
				    totalPage: parseInt(total_page),
				    callback: function(num) {
						$('#pageNo').val(num);
						var total_page=localStorage.getItem("total_page");
						$("#page").paging({
						    pageNo: parseInt(num),
						    totalPage: parseInt(total_page)
						})
						var paramData=getParam();
						getDataPolicy(paramData);
						$("#page").stopPagination({});
				    }
				})
			}
			html+='</table>';
			$('#data').html(html);
			
		}

	});
}

/* 下载文件 */
function download(type,id){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
		// var download = $("#download");
		
		var userToken = localStorage.getItem("userToken");
		//培训记录
		if(type==1){
			$.ajax({
				type:"get",
				url:site_url+"/trains?id="+id,
				headers:{'token':userToken},
				success:function(data){
					// window.open(data.data.file);
					var url=data.data.file;
					ajax(url, function(xhr) {
					    var filename = 'train.pdf'; // 自定义文件名+后缀
						filename=url.substr(57,20);
					    downloadFile(xhr.response, filename)
					}, {
					    responseType: 'blob'
					})
				}
			});
		}
		//政策文件
		else if(type==2){
			
		}
	})
	
}

/* 下载政策文件 */
function downloadPolicy(url){
	layui.use(['element', 'layer'], function() {
	   // window.open(url);
		// var url = 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=20550366,3650143321&fm=26&gp=0.jpg' // demo图片
		ajax(url, function(xhr) {
		    var filename = 'test.pdf'; // 自定义文件名+后缀
			filename=url.substr(58,20);
		    downloadFile(xhr.response, filename);
		}, {
		    responseType: 'blob'
		})
		// window.location.href = url;
		// layer.alert("下载成功！", {icon: 6});
	});
}
function downloadFile(content, filename) {
    var a = document.createElement('a');
    var blob = new Blob([content]);
    var url = window.URL.createObjectURL(blob);
    a.href = url;
    a.download = filename;
    a.click();
    window.URL.revokeObjectURL(url);
}

function download1() {
    var url = 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=20550366,3650143321&fm=26&gp=0.jpg' // demo图片
    ajax(url, function(xhr) {
        var filename = 'xxx.' + url.replace(/(.*\.)/, '') // 自定义文件名+后缀
        downloadFile(xhr.response, filename)
    }, {
        responseType: 'blob'
    })
}

function ajax(url, callback, options) {
    window.URL = window.URL || window.webkitURL
    var xhr = new XMLHttpRequest()
    xhr.open('get', url, true)
    if (options.responseType) {
        xhr.responseType = options.responseType
    }
    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4 && xhr.status === 200) {
            callback(xhr)
        }
    }
    xhr.send()
}
/* 反馈的保存按钮 */
function feedbackEditSave(){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
		
		var department=$("#departmentEdit").val();    //部门
		var staff=$("#userEditFeedback").val();              //员工
		
		var data = {
			finishId:staff,
			id:$("#feedbackId").val()
		}
		var userToken = localStorage.getItem("userToken");
		$.ajax({
			url:site_url+"/handclaps",
			type:"PUT",
			dataType:"json",
			contentType: 'application/json; charset=utf-8',
			data: JSON.stringify(data),
			headers:{'token':userToken},
			success:function(data){
				layer.alert("保存成功！", {icon: 6});
				window.location.reload();
			}
		})
		
		
	});
}
/* 添加的关闭按钮 */
function addClose(){
	location.reload();
	$("#addDiv").hide();
}
/* 编辑的关闭按钮 */
function editClose(){
	location.reload();
	$("#editDiv").hide();
}
/* 政策文件编辑的关闭按钮 */
function policyEditClose(){
	var param=getParam();
	getDataPolicy(param);
	$("#editDiv").hide();
}
/* 文档预览的关闭按钮 */
function fileClose(){
	$("#fileDiv").hide();
}
/* 政策文件添加的关闭按钮 */
function policyAddClose(){
	$("#pdfPreview").hide();
	$("#addDiv").hide();
}
/* 问题反馈功能的关闭按钮 */
function feedbackClose(){
	
	$("#feedback_img").html("");
	$("#editDiv").hide();
}
/* 文件导入 */
function fileImport(){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
	    // layer.alert("导入！"+id, {icon: 6});
		document.getElementById("importForm").submit();
	});	
}
/* 工资明细的数据切换 */
function changeData(id){
	// alert(id);
	switch(id){
		case 1:
			$("#title_one").removeClass("tab-div-two");
			$("#title_one").addClass("tab-div-one");
			$("#title_two").removeClass("tab-div-one");
			$("#title_two").addClass("tab-div-two");
			
			$("#data_one").show();
			$("#data_two").hide();
		break;
		case 2:
			$("#data_one").hide();
			$("#data_two").show();
		break;
	}
	
}
/* 搜索功能 */
function search(type){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
		$('#pageNo').val("1");
		var project=$('#search_project').val();
		var department=$('#search_department').val();
		var date_start=$('#search_date_start').val();
		var date_end=$('#search_date_end').val();
		var keyword=$('#search_keyword').val();
		if(keyword.length>10){
			layer.alert("超过字数长度！"+date_start, {icon: 6});
			return;
		}
		
		var param = {
	    	current:$('#pageNo').val(),
	     	deptId:department==undefined?"":department,
	     	start:date_start,
	     	end:date_end,
	     	keyword:keyword
	 	};
		
		switch(type){
	    	//重要通知
	    	case 2:
	    		getDataNotice(param);
	    		break;	
	    	//问题反馈(反馈列表)
	    	case 61:
	    		getDataFeedback(param);
	    		break;	
	    	//问题反馈(已配任务)
	    	case 62:
	    		getDataTask(param);
	    		break;	
	    	//招聘计划
	    	case 8:
	    		getDataRecruit(param);
	    		break;	
	    	//政策文件
	    	case 9:
	    		getDataPolicy(param);
	    		break;
    		//设备申请
	    	case 10:
	    		getDataEquipment(param);
	    		break;
	    	//培训记录
	    	case 11:
	    		getDataTrain(param)
	    }	
		
	});	
}

var num=0;
var trainImg=[];
var files = [];
/* 文件选择 */
function fileChange(type,file){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
		  if (!file.files || !file.files[0]){
			  return;
		  }
		  var name=file.files[0].name;
		  var extName = name.substring(name.lastIndexOf(".")+1).toLowerCase();
		  // alert(extName);
		 
		switch(type){
			//政策文件的选择上传文档
			case 1:
				if(extName=="pdf"){
				}else{
					layer.alert("文件格式不正确！", {icon: 5});
					return;
				}
				//pdf的本地路径
				// $('#fileSrc').attr("href","http://192.168.0.193/Property/test.pdf");
				$('#pdfPreview').show();
				break;	
			//培训记录的选择图片上传
			case 2:
				if(extName=="jpg" || extName=="png" || extName=="jpeg"){
				}else{
					layer.alert("图片格式不正确！", {icon: 5});
					return;
				}
				var windowURL = window.URL || window.webkitURL;
				var fileObj =  file;
				var dataURL;
				
				dataURL = windowURL.createObjectURL(fileObj.files[0]);
				
				
				/* 保存的图片 */
				var itemSave=JSON.parse(localStorage.getItem("imageArray"));
				var imgArray={};
				
				var num=trainImg.length;
				files[num] = fileObj.files[0];
				var reader = new FileReader();
			    var imgUrlBase64 = reader.readAsDataURL(fileObj.files[0]);
		        var head_img = "";
		        reader.onload = function (e) {
		        	head_img = reader.result;
		        	// files[num] = head_img;
		        }
				trainImg[num]=dataURL;
				getImgData();
				break;
		}	
	});	
}
/* 培训记录的图片显示 */
function getImgData(){
	var html='';
	html+='<div class="policy-download">';
	html+='<label for="uploadImgAdd"><img src="img/add_icon.png" /></label></br>';
	html+='<input id="uploadImgAdd" type="file" accept="image/jpg,image/jpeg,image/png" style="display: none;" onchange="fileChange(2,this);"/>';
	html+='<label>点击上传图片</label>';
	html+='</div>';
	// alert("图片张数："+trainImg.length);
	for(var i=0;i<trainImg.length;i++){
		html+='<div style="float: left;">';
		html+='<img class="train-item-img" src="'+trainImg[i]+'" />';
		html+='<img src="img/black_close.png" style="margin-left: -15px;" onclick="dateDelete(4,'+i+');"/>';
		html+='</div>';
	}
	// alert(html);
	$("#trainImgsAdd").html(html);
}
/* 获取文件的路径 */
 function getFullPath(obj)
{
    if(obj)
 {
     if (window.navigator.userAgent.indexOf("MSIE")>=1)    //ie
     {
         // obj.();
         return document.ion.createRange().text;
     }
     else if(window.navigator.userAgent.indexOf("Firefox")>=1)             //firefox
     {
         if(obj.files)
         {
             return obj.files.item(0).getAsDataURL();
         }
         return obj.value;
     }
     return obj.value;
  }
}
/* 政策文件的添加保存按钮 */
function policyAddSave(){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
		var theme=$('#theme').val();        //标题
		var upload=$('#upload').val();      //上传者
		var weight=$('#weight').val();      //权重
		var uploadFile=$('#uploadFile')[0].files;
		// var formData = new FormData($('#uploadForm')[0]); 
		// layer.alert("文档："+uploadFile);
		if(theme==""){
			layer.alert("标题不能为空！", {icon: 5});
			return;
		}
		if(upload==""){
			layer.alert("上传者不能为空！", {icon: 5});
			return;
		}
		if(weight==""){
			layer.alert("权重不能为空！", {icon: 5});
			return;
		}
		if($('#uploadFile').val()==""){
			layer.alert("文档不能为空！", {icon: 5});
			return;
		}
		  
		var formData = new FormData();
		formData.append("title",theme);
		formData.append("userName",upload);
		formData.append("weight",weight);
		formData.append("file",uploadFile[0]);
		
		var token = localStorage.getItem("userToken");
		  
		$.ajax({
		  	type:"POST",
		  	url:site_url+"/policys",
		  	headers:{'token':token},
		  	dataType:'multipart/form-data',
			data: formData,
		  	cache: false, // 不缓存
		  	processData: false, // jQuery不要去处理发送的数据
		  	contentType: false, // jQuery不要去设置Content-Type请求头
			async: false,
			success:function(data){
				// alert(data.code);
				if(data.code==0){
					window.location.reload();
				}else{
					layer.alert(data.msg, {icon: 5});
				}
			}
		});
		window.location.reload();
	});	
}
/* 政策文件的编辑保存按钮 */
function policyEditSave(){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
		  var theme=$('#themeEdit').val();        //标题
		  var upload=$('#uploadEdit').val();      //上传者
		  var weight=$('#weightEdit').val();      //权重
		  var uploadFile=$('#uploadFileEdit')[0].files;
		  var fileSrc = $("#fileSrc").attr('href');
		  var id = $("#policyIdEdit").val();
		  if(theme==""){
			  layer.alert("标题不能为空！", {icon: 5});
			    return;
		  }
		  if(upload==""){
		  		layer.alert("上传者不能为空！", {icon: 5});
		  		return;
		  }
		  if(weight==""){
		  		layer.alert("权重不能为空！", {icon: 5});
		  		return;
		  }
		var formData = new FormData();
		formData.append("title",theme);
		formData.append("userName",upload);
		formData.append("weight",weight);
		if (uploadFile) {
			formData.append("file",uploadFile[0]);
		}else{
			formData.append("fileUrl",fileSrc);
		}
		formData.append("id",id);
		var token = localStorage.getItem("userToken");
		$.ajax({
		  	type:"POST",
		  	url:site_url+"/policys",
		  	headers:{'token':token},
		  	dataType:'multipart/form-data',
			data: formData,
		  	cache: false, // 不缓存
		  	processData: false, // jQuery不要去处理发送的数据
		  	contentType: false, // jQuery不要去设置Content-Type请求头
			success:function(data){
				if(data.code==0){
					window.location.reload();
				}else{
					layer.alert(data.msg, {icon: 5});
				}
			}
		});
		window.location.reload();
	});	
}
/* 预览文档 */
function previewPdf(){
	
	var src=$('#fileSrcSave').val();
	var strArray=src.split("?");
	// window.open(src);
	$('a.media').attr("href",strArray[0]);
	// $('#fileSrc').attr("href",src);
	
    // $(".pdf").media();  
	$('a.media').media({width:800, height:530});
	$('#fileDiv').show();
}
//-----------------------------------------设备申请开始-----------------------------------------------
//给设备申请页面赋值
function getDataEquipment(param){
	 
	var userToken=localStorage.getItem("userToken");
	var URL = site_url+"/deviceapplys/page?size=10&current="+param.current+"&deptId="+param.deptId+"&start="+param.start+"&end="+param.end+"&userName="+param.keyword;
	$.ajax({
		type:"get",
		url:URL,
		headers:{'token':userToken},
		success:function(data){
			$('#data').html("");
			var result = data.data.records;
			var html="";
			html+='<table>';
			html+='<tr class="table-top">';
			html+='<td width="10%">部门</td><td width="8%">姓名</td><td width="15%">联系方式</td><td width="10%">借出物品</td><td width="15%">借出时间</td>';
			html+='<td width="15%">归还时间</td><td width="15%">申请原因</td><td width="6%">状态</td><td width="6%">操作</td></tr>';
		
			for (var i=0;i<result.length;i++) {
				if(i%2==0){
					html+='<tr class="item-white" >';
				}else{
					html+='<tr class="item-blue">';
				}
				html+='<td>'+(result[i].deptName==null?"":result[i].deptName)+'</td>';
				html+='<td>'+(result[i].deptName==null?"":result[i].userName)+'</td>';
				html+='<td>'+(result[i].deptName==null?"":result[i].phone)+'</td>';
				html+='<td>'+result[i].loanedGoods+'</td>';
				html+='<td>'+result[i].loanedTime+'</td>';
				html+='<td>'+result[i].backTime+'</td>';
				html+='<td>'+result[i].reason+'</td>';
				if(result[i].state == 0){
					html+='<td style="color:red">未还</td>';
				}else{
					html+='<td>已还</td>';
				}
				html+='<td>';
				html+='<img src="img/edit_icon.png" onclick="dateEdit(6,\''+result[i].id+'\')"/>';
				html+='</td>';
				html+='</tr>';
			}
			if(result.length<=0){
				html+='<tr class="item-white">';
				html+='<td colspan="9"><div>暂无数据</div></td>';
				html+='</tr>';
			}else{
				var pageNo=data.data.current;
				var total_page=data.data.pages;
				localStorage.setItem("total_page",total_page);
				//分页
				$("#page").paging({
				    pageNo: parseInt(pageNo),
				    totalPage: parseInt(total_page),
				    callback: function(num) {
						$('#pageNo').val(num);
						var total_page=localStorage.getItem("total_page");
						$("#page").paging({
						    pageNo: parseInt(num),
						    totalPage: parseInt(total_page)
						})
						var paramData=getParam();
						getDataEquipment(paramData);
						$("#page").stopPagination({});
				    }
				})
			}
			html+='</table>';
			$('#data').html(html);
		}
	});
	
}
/* 设备申请的保存 */
function equipAddSave(){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
		  var department=$('#department').val();      //部门
		  var username=$('#username').val();          //姓名
		  var phone=$('#phone').val();                //联系电话
		  var borrow=$('#borrow').val();              //借出物品
		  var borrowTime=$('#borrowTime').val();      //借出时间
		  var returnTime=$('#returnTime').val();      //归还时间
		  var status=$('#status').val();              //状态
		  var reason=$('#reason').val();              //原因
		
		  if(department==""){
			  layer.alert("部门不能为空！", {icon: 5});
			    return;
		  }
		  if(username==""){
		  		layer.alert("姓名不能为空！", {icon: 5});
		  		return;
		  }
		  if(phone==""){
		  		layer.alert("联系电话不能为空！", {icon: 5});
		  		return;
		  }
		  if(borrow==""){
		  		layer.alert("借出物品不能为空！", {icon: 5});
		  		return;
		  }
		  if(borrowTime==""){
		  		layer.alert("借出时间不能为空！", {icon: 5});
		  		return;
		  }
		  if(returnTime==""){
		  		layer.alert("归还时间不能为空！", {icon: 5});
		  		return;
		  }
		  if(status==""){
		  		layer.alert("状态不能为空！", {icon: 5});
		  		return;
		  }
		  if(reason==""){
		  		layer.alert("申请原因不能为空！", {icon: 5});
		  		return;
		  }
		  
		  var data={
		  	userId:username,
		  	loanedGoods:borrow,
		  	loanedTime:borrowTime,
		  	backTime:returnTime,
		  	reason:reason,
		  	state:status
		  }
		   var userToken = localStorage.getItem("userToken");
		  $.ajax({
		  	type:"POST",
		  	url:site_url+"/deviceapplys",
		  	dataType:"json",
			contentType:"application/json",
            data: JSON.stringify(data),
            headers:{'token':userToken},
            success:function(data){
            	if (data.data) {
            		// layer.alert("申请成功！", {icon: 6});
					location.reload(); 
            	}
            }
		  });
	});	
}
/* 设备申请的保存 */
function equipEditSave(){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
		  var department=$('#departmentEdit').val();      //部门
		  var username=$('#usernameEdit').val();          //姓名
		  var phone=$('#phoneEdit').val();                //联系电话
		  var borrow=$('#borrowEdit').val();              //借出物品
		  var borrowTime=$('#borrowTimeEdit').val();      //借出时间
		  var returnTime=$('#returnTimeEdit').val();      //归还时间
		  var status=$('#statusEdit').val();              //状态
		  var reason=$('#reasonEdit').val();              //原因
		
		  if(department==""){
			  layer.alert("部门不能为空！", {icon: 5});
			    return;
		  }
		  if(username==""){
		  		layer.alert("姓名不能为空！", {icon: 5});
		  		return;
		  }
		  if(phone==""){
		  		layer.alert("联系电话不能为空！", {icon: 5});
		  		return;
		  }
		  if(borrow==""){
		  		layer.alert("借出物品不能为空！", {icon: 5});
		  		return;
		  }
		  if(borrowTime==""){
		  		layer.alert("借出时间不能为空！", {icon: 5});
		  		return;
		  }
		  if(returnTime==""){
		  		layer.alert("归还时间不能为空！", {icon: 5});
		  		return;
		  }
		  if(status==""){
		  		layer.alert("状态不能为空！", {icon: 5});
		  		return;
		  }
		  if(reason==""){
		  		layer.alert("申请原因不能为空！", {icon: 5});
		  		return;
		  }
		  
		  var data={
		  	id:$("#deviceapplys_id").val(),
		  	userId:username,
		  	loanedGoods:borrow,
		  	loanedTime:borrowTime,
		  	backTime:returnTime,
		  	reason:reason,
		  	state:status
		  }
		   var userToken = localStorage.getItem("userToken");
		  $.ajax({
		  	type:"PUT",
		  	url:site_url+"/deviceapplys",
		  	dataType:"json",
			contentType: 'application/json; charset=utf-8',
            data: JSON.stringify(data),
            headers:{'token':userToken},
            success:function(data){
            	if (data.data) {
            		layer.alert("编辑成功！", {icon: 6});
					window.location.reload();
            	}
            }
		  });

	});	
}
//-----------------------------------------设备申请结束-----------------------------------------------
//-----------------------------------------培训记录开始-----------------------------------------------
//给培训记录页面赋值
function getDataTrain(param){
	
	var URL = site_url+"/trains/page?size=10&current="+param.current+"&title="+param.keyword;
	var userToken = localStorage.getItem("userToken");
	$.ajax({
		type:"get",
		url:URL,
		headers:{'token':userToken},
		success:function(data){
			$('#data').html("");
			var result = data.data.records;
			var html="";
			html+='<table>';
			html+='<tr class="table-top">';
			html+='<td width="15%">序号</td><td width="20%">课题</td><td width="30%">培训人员</td><td width="20%">培训时间</td><td width="15%">操作</td></tr>';
		
			for (var i=0;i<result.length;i++) {
				if(i%2==0){
					html+='<tr class="item-white" >';
				}else{
					html+='<tr class="item-blue">';
				}
				html+='<td>'+result[i].id+'</td>';
				html+='<td>'+result[i].title+'</td>';
				html+='<td>'+result[i].trainName+'</td>';
				html+='<td>'+result[i].createTime+'</td>';
				html+='<td>';
				html+='<img id="check" src="img/check_icon.png" onclick="infoDiv(\''+result[i].id+'\');"/>';
				html+='<img id="download" style="margin-left:10px;" src="img/download_bule_icon.png" onclick="download(1,\''+result[i].id+'\')"/>';
				html+='<img id="delete" style="margin-left: 10px;" src="img/delete_icon.png" onclick="dateDelete(3,\''+result[i].id+'\')"/>';
				html+='</td>';
				html+='</tr>';
			}
			if(result.length<=0){
				html+='<tr class="item-white">';
				html+='<td colspan="5"><div>暂无数据</div></td>';
				html+='</tr>';
			}else{
				var pageNo=data.data.current;
				var total_page=data.data.pages;
				localStorage.setItem("total_page",total_page);
				//分页
				$("#page").paging({
				    pageNo: parseInt(pageNo),
				    totalPage: parseInt(total_page),
				    callback: function(num) {
						$('#pageNo').val(num);
						var total_page=localStorage.getItem("total_page");
						$("#page").paging({
						    pageNo: parseInt(num),
						    totalPage: parseInt(total_page)
						})
						var paramData=getParam();
						getDataTrain(paramData);
						$("#page").stopPagination({});
				    }
				})
			}
			html+='</table>';
			$('#data').html(html);
		}
	})
}
/* 培训记录查看页面 */
function infoDiv(id){
	var userToken = localStorage.getItem("userToken");
	
	$.ajax({
		type:"get",
		url:site_url+"/trains?id="+id,
		headers:{'token':userToken},
		success:function(data){
			$('#topicInfo').val(data.data.title);         //课题
			$('#trainersInfo').val(data.data.userName);   //培训人员
			$('#trainDateInfo').val(data.data.createTime); //培训时间
			$('#traineesInfo').html(data.data.trainName);    //受训人员
			$('#fileSrcSave').val(data.data.file);
			 
			var img = data.data.img.split(",");
			$("#trainImgsInfo").html("");
			for(var i=0;i<img.length;i++){
				var querynames = $('<img style="width:80px; margin-left: 20px;" src="'+img[i]+'"/>');
				$("#trainImgsInfo").append(querynames);
			}
			
		}
	});
	
	$("#infoDiv").show();
}
/* 查看页面 */
function closeInfoDiv(){
	location.reload();
	$("#infoDiv").hide();
}
/* 部门关闭 */
function departClose(){
	$("#departDiv").hide();
}
/* 查看部门 */
function departOpen(){
	var userInfo = getUserInfo();
	var dept = getDeptById(userInfo.deptId);
	
	$('#train-depart').html("");
	var html="";
	for(var i = 0;i<dept.length;i++){
		if (dept[i].type ==3) {
			
			html+='<div class="sys-left-depart-item">';								
			html+='<input type="checkbox" name="trainDataId" style="margin-top:10px;" value="'+dept[i].name+'">';
			html+='<div class="sys-depart-txt" title="'+dept[i].name+'">'+dept[i].name+'</div>';
			html+='</div>';
		}
	}	
	$('#train-depart').html(html);
	$("#departDiv").show();
}
/* 培训记录的保存 */
function trainAddSave(){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
	  	var topicInfo=$('#topic').val();         //课题
	  	var trainersInfo=$('#trainersAdd').val();   //培训人员
	  	var trainDateInfo=$('#trainDateAdd').val(); //培训时间
	  	var traineesInfo=$('#traineesAdd').html();    //受训人员
		var pdf = $("#uploadFileAdd")[0].files[0];

		var uploadImgAdd = $("#uploadImgAdd")[0].files;
		
	  	if(topicInfo==""){
		  	layer.alert("课题不能为空！", {icon: 5});
	    	return;
	  	}
	  	if(trainersInfo==""){
	  		layer.alert("培训人员不能为空！", {icon: 5});
	  		return;
	  	}
	  	if(trainDateInfo==""){
	  		layer.alert("培训时间不能为空！", {icon: 5});
	  		return;
	  	}
	  	if(traineesInfo==""){
	  		layer.alert("受训人员不能为空！", {icon: 5});
	  		return;
	  	}
		if(files.length==0){
			layer.alert("图片不能为空！", {icon: 5});
			return;
		}
		if($("#uploadFileAdd").val()==""){
			layer.alert("上传教案不能为空！", {icon: 5});
			return;
		}
		
		var formData = new FormData();
		for(var i=0;i<files.length;i++){
			formData.append("pic",files[i])
		}
		formData.append("title",topicInfo);
		formData.append("userName",trainersInfo);
		formData.append("createTime",trainDateInfo);
		formData.append("trainName",traineesInfo);
		formData.append("file",pdf);
	    var token = localStorage.getItem("userToken");
	    
	    $.ajax({
	    	type:"POST",
	    	url:site_url+"/trains",
	    	headers:{'token':token},
	    	dataType:'multipart/form-data',
            data: formData,
			async : false,
			cache: false, // 不缓存
			processData: false, // jQuery不要去处理发送的数据
			contentType: false, // jQuery不要去设置Content-Type请求头
			/* xhr: function() {        //ajax进度条，直接拿过去就可以用
                    var xhr = $.ajaxSettings.xhr();
                    if (xhr.upload) {
                        xhr.upload.addEventListener("progress", function (e) {
                            var loaded = e.loaded; //已经上传大小情况
                            var tot = e.total; //附件总大小
                            var per = Math.floor(100 * loaded / tot); //已经上传的百分比
                            console.log(per)
                            // var percent=Math.floor((e.loaded / e.total)*100);//计算百分比
                            layer.progress(per);
                        }, false);
                        return xhr;
                    }
                }, */
            success:function(data){
				if(data.code==0){
					layer.alert("培训记录上传成功！", {icon: 6});
					location.reload();
				}else{
					layer.alert(data.msg, {icon: 5});
				}
				
            }
	    });
		layer.alert("培训记录上传成功！", {icon: 6});
		location.reload();
	});	
}
/* 培训记录编辑的保存 */
function trainAddEdit(){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
		   var topic=$('#topicAdd').val();         //课题
		  var trainers=$('#trainersAdd').val();   //培训人员
		  var trainDate=$('#trainDateAdd').val(); //培训时间
		  var trainees=$('#traineesAdd').html();    //受训人员
		  var trainImgs=$('#trainImgsAdd').val(); //图片
		  
		  if(topic==""){
		  		layer.alert("课题不能为空！", {icon: 5});
		  		return;
		  }
		  if(trainers==""){
		  		layer.alert("培训人员不能为空！", {icon: 5});
		  		return;
		  }
		  if(trainDate==""){
		  		layer.alert("培训时间不能为空！", {icon: 5});
		  		return;
		  }
		  if(trainees==""){
		  		layer.alert("受训人员不能为空！", {icon: 5});
		  		return;
		  }

		  layer.alert("培训记录编辑成功！", {icon: 6});
	});	
}
//-----------------------------------------培训记录结束-----------------------------------------------


function getRecruitById(id){
	var URL = site_url+"/recruitmentplans?id="+id;
	
	var userToken = localStorage.getItem("userToken");
	
	$.ajax({
		type:"get",
		url:URL,
		headers:{'token':userToken},
		success:function(data){
			var result = data.data;
			$("#recruitIdEdit").val(id);
			$("#department-edit-recutit").val(result.deptId);       //所属部门
			$("#positionEdit").val(result.job);     //职位
			$("#educationEdit").val(result.education);  
			$("#salaryStartEdit").val(result.salary.split("-")[0]);   //月薪开始值
			$("#salaryEndEdit").val(result.salary.split("-")[1]);       //月薪结束值
			$("#recruitDateEdit").val(result.recruitmentTime);   //招聘时间
			$("#recruitNumEdit").val(result.peoples);     //招聘人数
			$("#workYearEdit").val(result.workYears);
			$("#descEdit").val(result.content);
		}
	});
}


//根据部门查用户
function getUserByDept(deptId){
	
	var deptId = $("#departmentSave").val()==null?$("#departmentEdit").val():$("#departmentSave").val();
	
	var userInfo = JSON.parse(localStorage.getItem("userInfo"));
	var URL = site_url+"/users/dept?deptId="+deptId+"&size="+50;
	var userToken=localStorage.getItem("userToken");
	$.ajax({
		type:"get",
		url:URL,
		headers:{'token':userToken},
		success:function(data){
			$("#username").html("");
			$("#usernameEdit").html("");
			$("#userEditFeedback").html("");
			var option = "";
			var option1 = "";
			var option2 = "";
			for (var i=0;i<data.data.records.length;i++) {
				option = $('<option value="'+data.data.records[i].id+'">'+data.data.records[i].name+'</option>');
				option1 = $('<option value="'+data.data.records[i].id+'">'+data.data.records[i].name+'</option>');
				option2 = $('<option value="'+data.data.records[i].id+'">'+data.data.records[i].name+'</option>');
				$("#username").append(option);
				$("#usernameEdit").append(option1);
				$("#userEditFeedback").append(option2);
			}
		}
	});
}

function userNameChange(type) {
	var userId = "";
	if(type==1){
		userId = $("#username").val();
	}else{
		userId = $("#usernameEdit").val();
	}
	
	var userToken=localStorage.getItem("userToken");
	$.ajax({
		url:site_url+"/users?id="+userId,
		dataType:"json",
		headers:{'token':userToken},
		success:function(data){
			if(type==1){
				$("#phone").val(data.data.phone);
			}else if(type==2){
				$("#phoneEdit").val(data.data.phone);
			}
		}
	})
	
}
/* 培训记录选择部门 */
function trainDepartSave(){
	// var chk_value = [];
	var str="";
	$('input[name="trainDataId"]:checked').each(function () {
		   str+=$(this).val()+"&nbsp;&nbsp;&nbsp;&nbsp;";
	});
	$("#traineesAdd").html(str);
	$("#departDiv").hide();
}